package com.dao.impl;


import com.dao.ManagerService;
import com.entity.Manager;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class ManagerServiceImpl implements ManagerService {

    @Override
    public int insert(Manager manager) throws SQLException {
        return 0;
    }

    @Override
    public List<Manager> queryData(Manager manager) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "select * from manager where id=?";

        return JdbcUtil.queryData(sql,Manager.class,manager.getId());
    }

    @Override
    public int update(Manager manager) throws SQLException {
        return 0;
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        return 0;
    }

	@Override
	public PageUtil<Manager> queryDataBypage(int currentPgae, int pageSize, Manager manager) throws IllegalAccessException, SQLException, InstantiationException {
		return null;
	}


	/**
	 * 管理员登录
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public boolean login(String username, String password) throws SQLException, InstantiationException, IllegalAccessException {
		String sql = "select * from manager where username = ?";
		List<Manager> managers = JdbcUtil.queryData(sql, Manager.class, new Object[]{username});
		if(managers.isEmpty()) {
			throw new RuntimeException("账号不存在");
		}

		Manager manager = managers.get(0);

		return manager.getPassword().equals(password);
	}

	/**
	 * 获取用户信息
	 * @param username
	 * @return
	 */
	@Override
	public Manager getUserInfo(String username) throws SQLException, InstantiationException, IllegalAccessException {
		String sql = "select * from manager where username = ? ";

		List<Manager> managers = JdbcUtil.queryData(sql, Manager.class, new Object[]{username});

		if (managers.isEmpty()) {
			return new Manager();
		}

		Manager manager = managers.get(0);
		manager.setPassword(null);

		return manager;
	}
}
